local BankLayer = class("BankLayer", function(scene)
	local bankLayer = display.newLayer(cc.c4b(0, 0, 0, 125))
	return bankLayer
end)

local PopWait = appdf.req(appdf.CLIENT_SRC .. "app.views.layer.other.PopWait")
local ExternalFun = appdf.req(appdf.EXTERNAL_SRC .. "ExternalFun")
local ClipText = appdf.req(appdf.EXTERNAL_SRC .. "ClipText")
local MultiPlatform = appdf.req(appdf.EXTERNAL_SRC .. "MultiPlatform")

BankLayer.BT_TAKE			= 2
BankLayer.BT_SAVE			= 3

BankLayer.CBT_SAVEORTAKE	= 5
BankLayer.CBT_TRANSFER		= 6
BankLayer.EDIT_TRANSFER_DST = 9
BankLayer.CBT_BY_ID			= 10
BankLayer.CBT_BY_NAME		= 11
BankLayer.BT_EXIT			= 12
BankLayer.BT_FORGET			= 15
BankLayer.EDIT_SAVEORTAKE	= 17


BankLayer.BT_BACK			= 20


--开通银行
BankLayer.BT_ENABLE_BACK	= 31

function BankLayer:ctor(scene)
	ExternalFun.registerNodeEvent(self)
	self._scene = scene
	local this = self
	local btcallback = function(ref, type)
		if type == ccui.TouchEventType.ended then
			ref:setScale(1)
			this:onButtonClickedEvent(ref:getTag(), ref)
		elseif type == ccui.TouchEventType.began then
			ref:setScale(yl.btscale)
			return true
		elseif type == ccui.TouchEventType.canceled then  
			ref:setScale(1)
		end
	end
	self.btcallback = btcallback
	
	local rootLayer, csbNode = ExternalFun.loadRootCSB("mainbank/BankLayer.csb", self)
	self.bank_main = csbNode:getChildByName("bank_main")
	self.bank_main:getChildByName("Button_2")
	:setTag(BankLayer.BT_EXIT)
	:addTouchEventListener(btcallback)

	ExternalFun.FixTopUI(csbNode)
	csbNode:setPositionY((display.height-750)/2)
	
	local qu_bt = self.bank_main:getChildByName("Button_2_0")
	local cun_bt = self.bank_main:getChildByName("Button_2_0_0")
	
	
	self.qu_Panel = self.bank_main:getChildByName("qu_Panel")
	self.cun_Panel = self.bank_main:getChildByName("cun_Panel")

	ExternalFun.FixScaleUI(self.qu_Panel)
	ExternalFun.FixScaleUI(self.cun_Panel)
	
	local btncallback = function(ref, type)
		if type == ccui.TouchEventType.ended then
			ref:setScale(1)
			this:onBtnCQClick(ref)
		elseif type == ccui.TouchEventType.began then
			ref:setScale(yl.btscale)
			return true
		elseif type == ccui.TouchEventType.canceled then  
			ref:setScale(1)
		end
	end

	
	self.qu_Panel:setVisible(false)
	self.cun_Panel:setVisible(true)
	qu_bt:setEnabled(true)
	cun_bt:setEnabled(false)
	self.btn_qu_clear = self.qu_Panel:getChildByName("clearbt")
	self.btn_cun_clear = self.cun_Panel:getChildByName("clearbt")
	
	

	
	qu_bt:addTouchEventListener(function(ref, type)
		if type == ccui.TouchEventType.ended then
			ExternalFun.playClickEffect( )
			ref:setScale(1)
			self.qu_Panel:setVisible(true)
			self.cun_Panel:setVisible(false)
			self:resetView()
			qu_bt:setEnabled(false)
			cun_bt:setEnabled(true)
		elseif type == ccui.TouchEventType.began then
			ref:setScale(yl.btscale)
			return true
		elseif type == ccui.TouchEventType.canceled then  
			ref:setScale(1)
		end
	end)
	
	cun_bt:addTouchEventListener(function(ref, type)
		if type == ccui.TouchEventType.ended then
			ExternalFun.playClickEffect( )
			ref:setScale(1)
			self.qu_Panel:setVisible(false)
			self.cun_Panel:setVisible(true)
			self:resetView()
			-- self:openBankHistory()
			qu_bt:setEnabled(true)
			cun_bt:setEnabled(false)
		elseif type == ccui.TouchEventType.began then
			ref:setScale(yl.btscale)
			return true
		elseif type == ccui.TouchEventType.canceled then  
			ref:setScale(1)
		end
	end)
	
	
	
	self:refreshCunQuText()
	self:initcunPanel()
	self:initquPanel()
	
	self.qu_Slider = self.qu_Panel:getChildByName("qu_Slider")
	self.cun_Slider = self.cun_Panel:getChildByName("cun_Slider")

	self:refreshSubSlider(self.cun_Slider,0)
	self:refreshSubSlider(self.qu_Slider,0)
	self.qu_Slider:addEventListener(function(sender, eventType)
		local tempUserInsureStr =(GlobalUserItem.lUserInsure * sender:getPercent() / 100) / appdf.bili
		self:refreshSubSlider(sender)
		self.edit_Bankerqu:setText(math.floor(tempUserInsureStr))
		
	end)
	
	self.cun_Slider:addEventListener(function(sender, eventType)
		local tempUserInsureStr =(GlobalUserItem.lUserScore * sender:getPercent() / 100) / appdf.bili
		self:refreshSubSlider(sender)
		self.edit_BankerCun:setText(math.floor(tempUserInsureStr))
		
	end)
	
end
function BankLayer:resetView()
	self:refreshSubSlider(self.qu_Slider,0)
	self:refreshSubSlider(self.cun_Slider ,0)
	self.edit_BankerCun:setText("")
	self.edit_Bankerqu:setText("")

end 
function BankLayer:refreshCunQuText()
	local UserScoreStr = ExternalFun.ChangeScore(GlobalUserItem.lUserScore, 2, ",")
	local UserInsureStr = ExternalFun.ChangeScore(GlobalUserItem.lUserInsure, 2, ",")
	UserScoreStr = string.gsub(UserScoreStr,",","/")
	UserInsureStr = string.gsub(UserInsureStr,",","/")
	self.qu_Panel:getChildByName("Text_2_1"):setString(UserScoreStr)
	self.qu_Panel:getChildByName("Text_2_1_0"):setString(UserInsureStr)
	
	self.cun_Panel:getChildByName("Text_2_1"):setString(UserScoreStr)
	self.cun_Panel:getChildByName("Text_2_1_0"):setString(UserInsureStr)
end
function BankLayer:refreshSubSlider(slider,percent)
	percent = percent or slider:getPercent()
	percent = math.floor(percent+0.5)
	slider:setPercent(percent)
	local bg = slider:getChildByName("ats_bg")
	local slider_ats = bg:getChildByName("slider_ats")
	local sliderSize = slider:getContentSize()
	slider_ats:setString(percent.."/")
	bg:setPositionX(sliderSize.width *percent*0.01)
	
end
function BankLayer:initcunPanel()
	local doBt = self.cun_Panel:getChildByName("cunok_btn")
	doBt:setTag(BankLayer.BT_SAVE)
	doBt:addTouchEventListener(self.btcallback)
	local cunTemp_field = self.cun_Panel:getChildByName("cunTemp_field")
	self.edit_BankerCun = ccui.EditBox:create(cunTemp_field:getContentSize(), ccui.Scale9Sprite:create(""))
	:setFontName("fonts/round_body.ttf")
	:setPlaceholderFontName("fonts/round_body.ttf")
	:setFontSize(cunTemp_field:getFontSize())
	--    :setPlaceholderFontSize(cunTemp_field:getPlaceholderFontSize())
	:setMaxLength(10)
	:setInputMode(cc.EDITBOX_INPUT_MODE_NUMERIC)
	--	:setInputFlag(cc.EDITBOX_INPUT_FLAG_PASSWORD)
	:setPlaceHolder("请输入存入金额")
	:addTo(self.cun_Panel)
	self.edit_BankerCun:registerScriptEditBoxHandler(function(event, editbox)
		if event == "return" then
			local tempstr = string.gsub(editbox:getText(), "([^0-9])", "")
			tempstr = tonumber(tempstr) or 0
			tempstr = tempstr * appdf.bili
			if tempstr > GlobalUserItem.lUserScore then
				tempstr = GlobalUserItem.lUserScore 
			end
			self:refreshSubSlider(self.cun_Slider,tempstr/GlobalUserItem.lUserScore*100)
			local userscore,_ = math.modf(tempstr/appdf.bili)
			editbox:setText(userscore)
			
		elseif event == "began" then
			
		end
	end)
	self.edit_BankerCun:setPosition(cunTemp_field:getPosition())
	local clearbt = self.cun_Panel:getChildByName("clearbt")
	clearbt:addTouchEventListener(function(ref, type)
		if type == ccui.TouchEventType.ended then
			ExternalFun.playClickEffect( )
			ref:setScale(1)
			self.edit_BankerCun:setText("")
			self:refreshSubSlider(self.cun_Slider,0)
		elseif type == ccui.TouchEventType.began then
			ref:setScale(yl.btscale)
			return true
		elseif type == ccui.TouchEventType.canceled then  
			ref:setScale(1)
		end
	end)
	
	local allbt = self.cun_Panel:getChildByName("all")
	allbt:addTouchEventListener(function(ref, type)
		if type == ccui.TouchEventType.ended then
			ExternalFun.playClickEffect( )
			ref:setScale(1)
			self:refreshSubSlider(self.cun_Slider,100)
			local userscore,_ = math.modf(ExternalFun.ChangeScore(GlobalUserItem.lUserScore, 2, ",",false,true))
			self.edit_BankerCun:setText(userscore)
		elseif type == ccui.TouchEventType.began then
			ref:setScale(yl.btscale)
			return true
		elseif type == ccui.TouchEventType.canceled then  
			ref:setScale(1)
		end
	end)
	cunTemp_field:removeFromParent()
end
function BankLayer:initquPanel()
	local doBt = self.qu_Panel:getChildByName("quok_btn")
	doBt:setTag(BankLayer.BT_TAKE)
	doBt:addTouchEventListener(self.btcallback)
	local cunTemp_field = self.qu_Panel:getChildByName("quTemp_field")
	self.edit_Bankerqu = ccui.EditBox:create(cunTemp_field:getContentSize(), ccui.Scale9Sprite:create(""))
	:setFontName("fonts/round_body.ttf")
	:setPlaceholderFontName("fonts/round_body.ttf")
	:setFontSize(cunTemp_field:getFontSize())
	--   :setPlaceholderFontSize(cunTemp_field:getPlaceholderFontSize())
	:setMaxLength(10)
	:setInputMode(cc.EDITBOX_INPUT_MODE_NUMERIC)
	--	:setInputFlag(cc.EDITBOX_INPUT_FLAG_PASSWORD)
	:setPlaceHolder("请输入取入金额")
	:addTo(self.qu_Panel)
	self.edit_Bankerqu:registerScriptEditBoxHandler(function(event, editbox)
		if event == "return" then
			local tempstr = string.gsub(editbox:getText(), "([^0-9])", "")
			tempstr = tonumber(tempstr) or 0
			tempstr = tempstr * appdf.bili
			if tempstr > GlobalUserItem.lUserInsure then
				tempstr = GlobalUserItem.lUserInsure 
			end
			
			self:refreshSubSlider(self.qu_Slider,tempstr/GlobalUserItem.lUserInsure*100)
			tempstr = tempstr / appdf.bili
			local userscore,_ = math.modf(tempstr)
			editbox:setText(userscore)
			
		elseif event == "began" then
			
		end
	end)
	self.edit_Bankerqu:setPosition(cunTemp_field:getPosition())
	local clearbt = self.qu_Panel:getChildByName("clearbt")
	clearbt:addTouchEventListener(function(ref, type)
		if type == ccui.TouchEventType.ended then
			ref:setScale(1)
			ExternalFun.playClickEffect( )
			self.edit_Bankerqu:setText("")
				self:refreshSubSlider(self.qu_Slider,0)
		elseif type == ccui.TouchEventType.began then
			ref:setScale(yl.btscale)
			return true
		elseif type == ccui.TouchEventType.canceled then  
			ref:setScale(1)
		end
	end)
	local allbt = self.qu_Panel:getChildByName("all")
	allbt:addTouchEventListener(function(ref, type)
		if type == ccui.TouchEventType.ended then
			ExternalFun.playClickEffect( )
			ref:setScale(1)
			self:refreshSubSlider(self.qu_Slider,100)
			local userscore,_ = math.modf(ExternalFun.ChangeScore(GlobalUserItem.lUserInsure, 2, ",",false,true))
			self.edit_Bankerqu:setText(userscore)
		elseif type == ccui.TouchEventType.began then
			ref:setScale(yl.btscale)
			return true
		elseif type == ccui.TouchEventType.canceled then  
			ref:setScale(1)
		end
	end)
	cunTemp_field:removeFromParent()
end



--按键监听
function BankLayer:onButtonClickedEvent(tag, sender)
	ExternalFun.playClickEffect( )
	if tag == BankLayer.BT_TAKE then
		self:onTakeScore()
		self.edit_Bankerqu:setText("")
	elseif tag == BankLayer.BT_SAVE then
		self:onSaveScore()
		self.edit_BankerCun:setText("")
	elseif tag == BankLayer.BT_EXIT then
		self._scene:onKeyBack()	
	end

	-- if sender == self.btn_qu_clear then

	-- elseif sender == self.btn_cun_clear then

	-- end
end

function BankLayer:onBtnCQClick(sender)
	
	if sender == self.btn_qu_clear then
		self.edit_Bankerqu:setText("")
	elseif sender == self.btn_cun_clear then
		self.edit_BankerCun:setText("")
	end
end

--取款操作
function BankLayer:onTakeScore()
	print("onTakeScore>>>>>>>>>")
	
	
	--参数判断
	local changenum =	string.gsub(self.edit_Bankerqu:getText(), ",", "")
	if changenum == nil or changenum == ""  then
		showToast(self, "请输入正确金额！", 2)
		return
	end

	local szScore = string.gsub(tonumber(changenum) * appdf.bili, "([^0-9])", "")
	
	if #szScore < 1 then
		showToast(self, "请输入操作金额！", 2)
		return
	end
	
	local lOperateScore = tonumber(szScore)
	if lOperateScore < 1 then
		showToast(self, "取款金额不能为0！", 2)
		return
	end
	
	if lOperateScore > GlobalUserItem.lUserInsure then
		showToast(self, "您银行游戏币的数目余额不足,请重新输入游戏币数量！", 2)
		return
	end
	
	self:showPopWait()
	self._scene._LogonFrame:sendTakeBank(lOperateScore)
end

--存款
function BankLayer:onSaveScore()
	print("onSaveScore>>>>>>>>>")
	
	
	--参数判断
	local changenum =	string.gsub(self.edit_BankerCun:getText(), ",", "")
	
	if changenum == nil or changenum == "" then
		showToast(self, "请输入正确金额！", 2)
		return
	end
	local szScore = string.gsub(tonumber(changenum) * appdf.bili, "([^0-9])", "")	
	print("onSaveScore>>>>>", changenum, szScore)
	szScore = string.gsub(szScore, "[.]", "")
	if #szScore < 1 then
		showToast(self, "请输入操作金额！", 2)
		return
	end
	
	local lOperateScore = tonumber(szScore)
	
	if lOperateScore < 1 then
		showToast(self, "存款金额不能为0！", 2)
		return
	end
	print("onSaveScore>>>>>", lOperateScore, GlobalUserItem.lUserScore)
	if lOperateScore > GlobalUserItem.lUserScore then
		showToast(self, "您所携带游戏币的数目余额不足,请重新输入游戏币数量!", 2)
		return
	end
	
	self:showPopWait()
	
	self._scene._LogonFrame:sendSaveBank(lOperateScore)
end


--操作结果
function BankLayer:onTakeCallBack(recjson)
	print("onBankCallBack>>>>>")
	self:dismissPopWait()
	if recjson.result == 1  then
		showToast(self._scene, "成功取款", 1)
	else
		showToast(self._scene, recjson.msg, 2)
	end


	
end
function BankLayer:refreshMoney()
	self:refreshCunQuText()
	--self:getParent():getParent()._gold:setString(string.formatNumberThousands(GlobalUserItem.lUserScore, true, "/"))
	self:refreshSubSlider(self.qu_Slider,0)
	self:refreshSubSlider(self.cun_Slider,0)
end
--操作结果
function BankLayer:onSaveCallBack(recjson)
	print("onBankCallBack>>>>>")
	self:dismissPopWait()
	if recjson.result == 1 then
		showToast(self._scene, "成功存款", 1)
	else
		showToast(self._scene, recjson.msg, 2)
	end
	

end


--显示等待
function BankLayer:showPopWait()
	self._scene:showPopWait()
end

--关闭等待
function BankLayer:dismissPopWait()
	self._scene:dismissPopWait()
end

function BankLayer:onEnterTransitionFinish()
	
	
end



return BankLayer 